package com.wgj.android.zfp;

import android.app.Service;
import android.content.Intent;
import android.nfc.Tag;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.google.gson.Gson;
import com.jiaai.xui.XUI;
import com.jiaai.xui.cache.XCache;
import com.jiaai.xui.utils.log.XLog;
import com.wgj.android.http.WeiXinEvents;
import com.wgj.android.http.weixin.WXFormatUtil;
import com.wgj.android.http.weixin.WXHttp;
import com.wgj.android.http.weixin.WeiXinCacheUtil;
import com.wgj.android.http.weixin.model.AddFriendsBody;
import com.wgj.android.http.weixin.model.BaseRequest;
import com.wgj.android.http.weixin.model.VerifyUser;
import com.wgj.android.http.weixin.model.WXInfo;
import com.wgj.android.http.weixin.model.WxFriendOfGroup;
import com.wgj.android.http.weixin.model.WxGroupBody;
import com.wgj.android.http.weixin.model.WxGroupBodySub;
import com.wgj.android.ui.activity.TxActivity;
import com.wgj.android.utils.MD5Util;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */

public class BatchAddService extends Service {

    final private List<String> list = new ArrayList<String>();
    private BaseRequest baseRequest = new BaseRequest();
    private WXInfo wxInfo = WeiXinCacheUtil.getInfo();
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String nickName = intent.getStringExtra("nickName");
        new Thread(new BatchAdd(nickName)).start();
        return super.onStartCommand(intent, flags, startId);
    }

    class BatchAdd implements Runnable{
        private String UserName;

        public BatchAdd(String userName) {
            UserName = userName;
        }

        @Override
        public void run() {
            baseRequest = new BaseRequest();
            baseRequest.setDeviceID("e" + (WXFormatUtil.getRand15Num()));
            baseRequest.setSid(wxInfo.getWxsid());
            baseRequest.setSkey(wxInfo.getSkey());
            baseRequest.setUin(Long.parseLong(wxInfo.getWxuin()));

            WxGroupBody wxGroupBody = new WxGroupBody();
            wxGroupBody.setBaseRequest(baseRequest);
            List<WxGroupBodySub> wxGroupBodySubs = new ArrayList<>();
            wxGroupBodySubs.add(new WxGroupBodySub(UserName));
            wxGroupBody.setList(wxGroupBodySubs);

            String url = "http://"+ wxInfo.getBaseUrl()+"/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=".concat(""+new Date().getTime());
            try {
                String body= WXHttp.obtain().syncPost(url, new Gson().toJson(wxGroupBody));
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(body);
                    JSONArray jsonArray = new JSONArray(jsonObject.get("ContactList").toString());
                    jsonObject = new JSONObject(jsonArray.get(0).toString());
                    String res = jsonObject.get("MemberList").toString();
                    jsonArray = new JSONArray(res);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject temp =new JSONObject(jsonArray.get(i).toString());
                        list.add(temp.getString("UserName"));
                    }

                    startAdd();
                } catch (Exception e) {
                    e.printStackTrace();
                    stopSelf();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        private void startAdd() {

            AddFriendsBody body = new AddFriendsBody();
            body.setBaseRequest(baseRequest);

            body.getSceneList().add(33);

            body.setOpcode((int)(2));

            body.setSkey(wxInfo.getSkey());

            for (String e : list){
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException ee) {
                    ee.printStackTrace();
                }
                body.getBaseRequest().setDeviceID("e" + (WXFormatUtil.getRand15Num()));
                body.getVerifyUserList().add(new VerifyUser(e));

                String url = "http://"+WeiXinCacheUtil.getInfo().getBaseUrl()+"/cgi-bin/mmwebwx-bin/webwxverifyuser?" +
                        "r=".concat("" + new Date().getTime()) +
                        "&lang=zh_CN" +
                        "&pass_ticket=".concat(wxInfo.getPass_ticket());
                try {
                    String bodys=WXHttp.obtain().syncPost(url, new Gson().toJson(body));
                    Log.e("BatchAddService",bodys);
                    if (bodys.contains("11") || bodys.contains("1")) {//群加好友失败
                        Log.e("BatchAddService",bodys);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            }
            EventBus.getDefault().post(new WeiXinEvents(WeiXinEvents.EventsType.ADD_FRIEDN_STOP));
        }
    }
}
